package April._27.work7;



import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        // a --> b --> c --> d --> e
        Node e = new Node("e");
        Node d = new Node("d", e);
        Node c = new Node("c", d);
        Node b = new Node("b", c);
        Node a = new Node("a", b);

        // a --> b --> c --> d --> e --> c --> d --> e --> ...
        e.next = c;

        boolean bool = hasCircle(a);
        System.out.println(bool);
    }


    private static boolean hasCircle(Node head) {
        HashMap<Node, Node> nodeNodeHashMap = new HashMap<>();
        while (head.next!=null){
            if(head.next == nodeNodeHashMap.get(head)){
                return true;//有环
            }else {
                nodeNodeHashMap.put(head,head.next);

            }
        }
        return false;
    }
}
